<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>电影详情</title>
    <link rel="stylesheet" href="/dev/plugins/bootstrap/css/bootstrap.css">
    <style>
        .movie-header {
            text-align: center;
            margin-bottom: 30px;
        }

        .movie-poster {
            max-width: 300px;
            margin-bottom: 20px;
        }

        .movie-info {
            margin-top: 20px;
        }

        .movie-info p {
            margin-bottom: 10px;
        }

        .btn-back {
            margin-top: 20px;
        }
    </style>
</head>
<body>

<div class="container">
    <div class="movie-header">
        <img src="" id="movie-poster" alt="电影海报" class="movie-poster">
        <h2 id="movie-title">电影名称</h2>
        <p id="movie-year">上映年份</p>
        <p id="movie-rating">评分: 8.5/10</p>
    </div>

    <div class="movie-info">
        <h4>导演</h4>
        <p id="movie-director">导演姓名</p>

        <a id="movie-torrent" href="#" class="btn btn-success" target="_blank">点击下载</a>
        <a id="star" href="#" class="btn btn-warning" onclick="star()">收藏</a>
    </div>

    <div class="btn-back">
        <a href="#" class="btn btn-primary" onclick="window.history.back();">返回</a>
    </div>
</div>

<script src="/dev/plugins/jquery/jquery.min.js"></script>
<script>

    var movieId;

    window.addEventListener('message', function (event) {
        if (event.data.type === 'detail') {
            const id = event.data.id;
            movieId = id
            loadMovieDetails(id)
        }
    });

    // 模拟从后端获取电影信息
    function loadMovieDetails(movieId) {
        var movieData

        // 这里可以用 Ajax 请求后端接口来获取电影详情
        $.get('/dev/movies/' + movieId, function (res) {
            if (res && res.data) {
                movieData = res.data
                $('#movie-poster').attr('src', movieData.post);
                $('#movie-title').text(movieData.name);
                $('#movie-year').text(`上映年份: ${movieData.year}`);
                $('#movie-rating').text(`评分: ${movieData.score}`);
                $('#movie-director').text(movieData.director);
                $('#movie-torrent').attr('href', movieData.torrent).text('点击下载');
            }
        })
    }

    // 收藏
    function star() {
        $.ajax({
            url: '/dev/userMovies/star',
            type: 'POST',
            data: JSON.stringify({'movieId': movieId}),
            contentType: 'application/json',
            headers: {
                'Authorization': localStorage.getItem('token')
            },
            success: function (res) {
                if (res.code == 1) { // 登录成功
                    alert("收藏成功")
                } else if (res.code == 0) {
                    alert(res.msg);
                }
            },
            error: function (err) {
                console.error("请求失败", err);
            }
        });
    }
</script>

</body>
</html>
